//
//  AmountModifyView.m
//  sensen_ios
//
//  Created by pro on 16/2/26.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "AmountModifyView.h"

@interface AmountModifyView ()


@end

@implementation AmountModifyView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
//        self.layer.borderColor = [[UIColor colorWithWhite:0.85 alpha:1] CGColor];
//        self.layer.borderWidth = 1;
//        self.layer.cornerRadius = 4;
        
        // 强制宽高比为了美观
        self.width = self.height * 10 / 3;
        
        [self _initViews];
    }
    
    return self;
}

#pragma mark -- 创建视图 --
- (void)_initViews {
    
    NSArray *texts = @[@"-", @"0", @"+"];
    CGFloat margin = 2;
    CGFloat contentWidth = self.width - margin * 2;
    NSArray *widths = @[@(contentWidth * 0.3), @(contentWidth * 0.4), @(contentWidth * 0.3)];
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat width = 0;
    CGFloat height = self.height;
    // 循环创建 label
    for (int i = 0; i < texts.count; i ++) {
        x = 0;
        for (int j = 0; j < i; j ++) {
            x += ([widths[j] floatValue] + margin);
        }
        width = [widths[i] floatValue];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
        label.userInteractionEnabled = YES;
        label.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
        label.font = FONT(14);
        label.text = texts[i];
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:label];
        
        label.tag = i + 1; // 用于获得label
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(modifyAction:)];
        [label addGestureRecognizer:tap];
        
        // 给 + 和 - 加大号字符
        if (label.tag != 2) {
            label.font = FONT(18);
            label.textColor = [UIColor darkGrayColor];
        }
    }
    
//    // 循环创建分割线
//    width = 1;
//    for (int i = 1; i < texts.count; i ++) {
//        x = 0;
//        for (int j = 0; j < i; j ++) {
//            x += [widths[j] floatValue];
//        }
//        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
//        line.backgroundColor = [UIColor colorWithWhite:0.85 alpha:1];
//        [self addSubview:line];
//    }
    
    
}


#pragma mark - 点击事件
// 点击 + 或 - 的事件
- (void)modifyAction:(UITapGestureRecognizer *)tap {
    switch (tap.view.tag) {
        case 1:
            [self _deleteAction];
            break;
        case 2:
            break;
        case 3:
            [self _addAction];
            break;
        default:
            break;
    }

    UILabel *amountLabel = (UILabel *)[self viewWithTag:2];
    NSInteger currentAmount = [amountLabel.text integerValue];
    if ([self.delegate respondsToSelector:@selector(amountDidModified:)]) {
        [self.delegate amountDidModified:currentAmount];
    }
}
// - 1
- (void)_deleteAction {
    
    UILabel *amountLabel = (UILabel *)[self viewWithTag:2];
    NSInteger currentAmount = [amountLabel.text integerValue];
    // 数量已经是0了，则不能再减少
    if (currentAmount <= 0) {
        return;
    }
    amountLabel.text = [NSString stringWithFormat:@"%li", currentAmount - 1];
    
}
// + 1
- (void)_addAction {
    
    UILabel *amountLabel = (UILabel *)[self viewWithTag:2];
    NSInteger currentAmount = [amountLabel.text integerValue];
    amountLabel.text = [NSString stringWithFormat:@"%li", currentAmount + 1];
}



#pragma mark - 重写set
- (void)setAmount:(NSInteger)amount {
    
    _amount = amount;
    UILabel *amountLabel = (UILabel *)[self viewWithTag:2];
    amountLabel.text = [NSString stringWithFormat:@"%li", (long)_amount];
}


#pragma mark - 全局方法
// 获得数量
- (NSInteger)getAmount {
    
    UILabel *amountLabel = (UILabel *)[self viewWithTag:2];
    
    return [amountLabel.text integerValue];
    
}


@end
